# coding=UTF-8
from django import newforms as forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from djopping_user.models import Registration

class LoginForm(forms.Form):
    username = forms.CharField(label=_(u'Usuario'), max_length=100)
    password = forms.CharField(label=_(u'Contraseña'), max_length=50, widget=forms.PasswordInput)

class SignupForm(forms.Form):
    first_name = forms.CharField(label=_(u'Nombre'), max_length=255)
    last_name = forms.CharField(label=_(u'Apellido'), max_length=255)
    username = forms.CharField(label=_(u'Usuario'), max_length=255)
    email = forms.EmailField(label=_(u'e-Mail'))

    def clean_username(self):
        data = self.cleaned_data
        try:
            user = User.objects.get(username=data['username'])
            raise forms.util.ValidationError(_(u'Ya existe alguien registrado con este usuario. Por favor escoja otro.'))
        except User.DoesNotExist, e:
            return data['username']

    def clean_email(self):
        data = self.cleaned_data
        try:
            user = User.objects.get(email=data['email'])
            raise forms.util.ValidationError(_(u'Ya existe alguien registrado con este e-mail. Por favor escoja otro.'))
        except User.DoesNotExist, e:
            return data['email']

class PasswordReminderUserForm(forms.Form):
    username = forms.CharField(label=_(u'Usuario'), max_length=255)

class PasswordReminderEmailForm(forms.Form):
    email = forms.EmailField(label=_(u'e-Mail'))

class InputRegistrationKeyForm(forms.Form):
    key = forms.CharField(label=_(u'Clave'), max_length=32)

    def clean_key(self):
        key = self.cleaned_data['key']
        if len(key) != 32:
            raise forms.ValidationError(_(u'Clave inválida.'))
        try:
            registration = Registration.objects.get(key=key, completed=False)
        except Registration.DoesNotExist:
            raise forms.ValidationError(_(u'Clave inválida.'))
        return key

class SetPasswordForm(forms.Form):
    password1 = forms.CharField(label=_(u'Contraseña'), max_length=50, widget=forms.PasswordInput)
    password2 = forms.CharField(label=_(u'Repetir Contraseña'), max_length=50, widget=forms.PasswordInput)

    def clean_password1(self):
        password = self.cleaned_data['password1']
        if len(password) < 6:
            raise forms.ValidationError(_(u'Su contraseña debe contener al menos 6 caractéres.'))
        different_chars = []
        for char in password:
            if char not in different_chars:
                different_chars.append(char)
        if len(different_chars) < 3:
            raise forms.ValidationError(_(u'Su contraseña debe constar de al menos 3 caractéres diferentes.'))
        return password

    def clean_password2(self):
        data = self.cleaned_data
        if data['password2'] != self.data['password1']:
            raise forms.ValidationError(_(u'Las contraseñas no coinciden.'))
        return data['password2']
